﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLKHO.Class;

namespace QLKHO.Forms
{
    public partial class frmMain : Form
    {
        private bool booDangNhap = false;
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            try {
                clsMain.flexConn.DatabaseConnection(false);
                if (clsMain.flexConn.fcConn != null)
                {
                    DangNhap();
                    Form frm = new frmDonVi();
                    KhoiTaoForms(frm);
                    frm = new frmKho();
                    KhoiTaoForms(frm);
                    frm = new frmQuiCach();
                    KhoiTaoForms(frm);
                    frm = new frmHangHoa();
                    KhoiTaoForms(frm);
                    frm = new frmNhanVien();
                    KhoiTaoForms(frm);
                    frm = new frmLoaiNhanVien();
                    KhoiTaoForms(frm);
                    frm = new frmPhieuNhapKho();
                    KhoiTaoForms(frm);
                    //frm = new frmChiTietPhieuNhap();
                    //KhoiTaoForms(frm);
                    frm = new frmLoaiHangHoa();
                    KhoiTaoForms(frm);
                    frm = new frmReport();
                    KhoiTaoForms(frm);
                    frm = new frmPhieuXuatKho();
                    KhoiTaoForms(frm);
                    frm = new frmBaoCaoXuatNhapTon();
                    KhoiTaoForms(frm);
                    frm = new frmBaoCaoXuatNhapTonKho();
                    KhoiTaoForms(frm);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("frmMain_Load\n" + ex.ToString());
            }
            
        }
        private void DangNhap()
        {
            try
            {
                if (booDangNhap == false)
                {
                    DangNhapToolStripMenuItem.Text = "Đăng xuất";
                }
                else
                {
                    booDangNhap = false;
                    clsMain.booLogin = false;
                    DangNhapToolStripMenuItem.Text = "Đăng nhập";
                    trvMenu.Nodes.Clear();
                }

                Form frm = new Forms.frmDangNhap();
                frm.ShowDialog();

                if (clsMain.booLogin == true)
                {
                    ConnectSystem();
                    booDangNhap = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Dang nhap Function\n" + ex.ToString());
            }
        }
        private void ConnectSystem()
        {
            try
            {
                string strSQLParent = "";
                string strSQLChild = "";
                strSQLParent = "select Code,Data,IconID from System_Menus";
                strSQLChild = "select Code,Data,IconID,Proc_ListView,Proc_SaveData,Proc_tags from System_Childs where Parent_Code = ";
                strSQLChild = "select Code,Data,IconID,Proc_ListView,Proc_SaveData,Proc_tags from System_Childs, QuyenNguoiDung where  QuyenNguoiDung.USER_CODE='" + clsMain.strUserLogin + "' and System_Childs.Code=QuyenNguoiDung.SYSCHILD_CODE and System_Childs.Parent_Code = ";
                clsMain cls = new clsMain();
                cls.AddDataToTreeView(strSQLParent, strSQLChild, trvMenu);

            }
            catch (Exception ex)
            {
                MessageBox.Show("Connect to system\n" + ex.ToString());
            }
        }

        private void trvMenu_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (trvMenu.SelectedNode.Tag != null)
                {
                    string strTag = Convert.ToString(trvMenu.SelectedNode.Tag);

                    int intIDForm = Convert.ToInt32(strTag);

                    if (clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].booActive == true)
                    {
                        int i = 0;
                        bool isShowFlag = false;
                        for (; i < tabMain.Tabs.Count; i++)
                        {
                            if (tabMain.Tabs[i].Tag == clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Tag)
                            {
                                if (tabMain.Tabs[i].Visible == false)
                                    tabMain.Tabs[i].Visible = true;
                                tabMain.SelectedTab =(DevComponents.DotNetBar.SuperTabItem) tabMain.Tabs[i];

                                isShowFlag = true;
                            }
                        }
                       
                        if (isShowFlag == false)
                        {
                            if (intIDForm != 13)//Code tạm
                            {
                                //Forms_QLHS.fcForms[intIDForm].fcForm.Load;
                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].booIsShow = true;
                                clsMain.flexConn.LoadListView(intIDForm);
                                clsMain.flexConn.GenerateCode(intIDForm);
                                clsMain.flexConn.ClearControls(intIDForm);
                                //Forms_QLHS.fcForms[intIDForm].fcForm.Visible=true;
                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.TopLevel = false;

                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Visible = true;
                                 clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.FormBorderStyle = FormBorderStyle.None;
                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Dock = DockStyle.Fill;
                                
                               DevComponents.DotNetBar.SuperTabItem newTab= tabMain.CreateTab(clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].strTitle);
                               DevComponents.DotNetBar.SuperTabControlPanel panel = (DevComponents.DotNetBar.SuperTabControlPanel)newTab.AttachedControl;
                                
                               panel.Controls.Add(clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm);
                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
                                tabMain.SelectedTab = newTab;
                                                               
                                tabMain.Tabs[tabMain.Tabs.Count - 1].Tag = clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Tag;
                                
                                //xtraTabControl.TabPages[xtraTabControl.TabPages.Count - 1].ImageIndex = trvMenu.SelectedNode.ImageIndex;

                                clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Show();

                               // toolStripStatusLabel2.Text = clsMain.flexConn.Forms_QLHS.fcForms[intIDForm].fcForm.Text;
                            }
                            else if (intIDForm == 13)//Code tạm
                            {
                                //xtreportDANHSACHNV xtr = new xtreportDANHSACHNV();
                               // xtr.ShowPreview();
                            }
                        }
                        
                    }


                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("treeview menu doubleclick\n" + ex.ToString());
            }
        }
        private void KhoiTaoForms(System.Windows.Forms.Form frm)
        {
            try
            {

                // frm.MdiParent = this;
                //Khởi tạo form
                clsMain.flexConn.InitForms(ref frm, clsMain.flexConn.fcConn);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Khởi tạo forms\n" + ex.ToString());
            }
        }

        private void tabMain_TabItemClose(object sender, DevComponents.DotNetBar.SuperTabStripTabItemCloseEventArgs e)
        {
            e.Tab.Visible = false;
            e.Cancel = true;
        }

        
    }
}

